package co.fitstart.scheduler.web.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.support.HttpResult;

import co.fitstart.scheduler.job.ReportJob;
import co.fitstart.scheduler.service.FinishedCourseReportService;
import co.fitstart.scheduler.service.RevenueReportService;
import co.fitstart.scheduler.service.VisitorReportService;


/**
 * 
 * @author haiyan_xiao
 *
 */
@Controller
public class ErrorRemindController {
	
	private static final Logger logger = LoggerFactory.getLogger(ReportJob.class);
    
    @SuppressWarnings("unused")
	@Autowired
    private RevenueReportService revenueReportService;
    
    @Autowired
    private FinishedCourseReportService finishedCourseReportService;
    
    @SuppressWarnings("unused")
    @Autowired
    private VisitorReportService visitorReportService;

    /**
     * 用于scheduler异常停止运行时提醒
     * @return
     */
    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public @ResponseBody String onCancelSchedule() {
        

        
        return "200";
    }
    
    
    @RequestMapping(value = "/finish-report", method = RequestMethod.GET)
    public @ResponseBody String finishReport(@RequestParam(name = "day") @DateTimeFormat(pattern="yyyy-MM-dd") Date day) {
        
    	HttpResult result = new HttpResult();
    	
    	try {
    		
    		logger.info("start running ReportJob#scanCreateFinishedCourseReport at {}", new Date());
            finishedCourseReportService.createFinishedCourseReport(day);
            logger.info("finish running ReportJob#scanCreateFinishedCourseReport at {}", new Date());
            
            result.success().message("更改成功");
            
    	} catch (Exception e) {
    		logger.error(e.getMessage());
    		result.fail("500").message("更改失败");
    	}
    	
    	return JsonUtils.toJsonString(result);
    }
}
